package com.infore.maintenance.main;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
//import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 服务入口方法类
 * @author wj
 */

@EnableCaching
@MapperScan({"com.infore.maintenance.main.dao"})
@SpringBootApplication(scanBasePackages = {"com.infore"})
@EnableFeignClients(basePackages = {"com.infore"})
@EnableSwagger2
@EnableDiscoveryClient
public class MaintenanceApplication extends SpringBootServletInitializer  implements CommandLineRunner {
    protected static Log log = LogFactory.getLog(MaintenanceApplication.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MaintenanceApplication.class);

    }

    public static void main(String[] args)  throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(MaintenanceApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path")==null? "":env.getProperty("server.servlet.context-path");
        log.info("\n----------------------------------------------------------\n" +
                "Application service is running! Access URLs:\n" +
                "Swagger文档: \thttp://" + ip + ":" + port + path + "/swagger-ui.html\n" +
                "----------------------------------------------------------");
    }
    /**
     * springboot运行后此方法首先被调用<br/>
     * 实现CommandLineRunner抽象类中的run方法
     */
    @Override
    public void run(String... args) throws Exception {
        System.out.println("-------------Maintenance service started successfully----------------");
    }
}

